-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic arithmetics #20212
Basic arithmetics #20212
Conversation
@bricelam draft for basic arithmetics and ef_negate |
{ | ||
typeof(decimal), | ||
typeof(TimeSpan), | ||
typeof(ulong) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should file an issue to do the ulong
ones too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it would make sense, yes. I'd be happy to work on this, when decimals are finished, if OK.
name: "ef_add", | ||
(decimal? left, decimal? right) => left.HasValue && right.HasValue | ||
? decimal.Add(left.Value, right.Value) | ||
: default(decimal?), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be...
(decimal? left, decimal? right) => left / right,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean left + right? Yes, it can - and is now.
return decimal.Divide(dividend.Value, divisor.Value); | ||
} | ||
|
||
throw new DivideByZeroException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't Divide just throw this?
Can all of this just be...
(decimal? dividend, decimal? divisor) => dividend / divisor,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, at least the tests passed this way. But it's anyway not needed here, as error checking happens down the path inside Decimal.Dec.Calc.cs
which I (see below comment) unfortunately did not realize earlier.
Fixed it.
name: "ef_multiply", | ||
(decimal? left, decimal? right) => left.HasValue && right.HasValue | ||
? decimal.Multiply(left.Value, right.Value) | ||
: default(decimal?), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto. left * right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
name: "ef_negate", | ||
(decimal? m) => m.HasValue | ||
? decimal.Negate(m.Value) | ||
: default(decimal?), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto. -m
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Didn't realize there were also operators...for whatever reason. Need to check more carefully next time. Thanks for the hints. |
@bricelam sorry for the delay, been a little chaotic here recently. So I gave the error messages related to the above failures a look, but so far lack any idea how to approach this. Apparently, the failing test |
@lokalmatador - That test has intermittent failure due to high load on SqlServer. I recently disabled that test from running. Can you rebase on latest master? The error should go away. It is not an issue in your PR. |
@smitpatel Oh yeah, I can see it now. Thanks for the info. |
Is there anything open on this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need functional tests which performs decimal operations in query and assert generated SQL with it.
_ => visitedExpression | ||
}; | ||
|
||
string ResolveFunctionNameFromExpressionType(ExpressionType expressionType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@smitpatel Who will add those? Is it supposed to be me? If yes, maybe you can elaborate a little more on it? |
@maumar - Can you help with testing here? |
Ping @maumar |
@lokalmatador sorry for the super late reply. For the test, please add the following snippet [ConditionalFact]
public virtual void Projecting_aritmetic_operations_on_decimals()
{
using var context = CreateContext();
var expected = (from dt1 in context.Set<BuiltInDataTypes>().ToList()
from dt2 in context.Set<BuiltInDataTypes>().ToList()
orderby dt1.Id, dt2.Id
select new
{
add = dt1.TestDecimal + dt2.TestDecimal,
subtract = dt1.TestDecimal - dt2.TestDecimal,
multiply = dt1.TestDecimal * dt2.TestDecimal,
divide = dt1.TestDecimal / dt2.TestDecimal,
negate = -dt1.TestDecimal
}).ToList();
Fixture.TestSqlLoggerFactory.Clear();
var actual = (from dt1 in context.Set<BuiltInDataTypes>()
from dt2 in context.Set<BuiltInDataTypes>()
orderby dt1.Id, dt2.Id
select new
{
add = dt1.TestDecimal + dt2.TestDecimal,
subtract = dt1.TestDecimal - dt2.TestDecimal,
multiply = dt1.TestDecimal * dt2.TestDecimal,
divide = dt1.TestDecimal / dt2.TestDecimal,
negate = -dt1.TestDecimal
}).ToList();
Assert.Equal(expected.Count, actual.Count);
for (var i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i].add, actual[i].add);
Assert.Equal(expected[i].subtract, actual[i].subtract);
Assert.Equal(expected[i].multiply, actual[i].multiply);
Assert.Equal(expected[i].divide, actual[i].divide);
Assert.Equal(expected[i].negate, actual[i].negate);
}
AssertSql(
@"SELECT ef_add(""b"".""TestDecimal"", ""b0"".""TestDecimal"") AS ""add"", ef_add(""b"".""TestDecimal"", ef_negate(""b0"".""TestDecimal"")) AS ""subtract"", ef_multiply(""b"".""TestDecimal"", ""b0"".""TestDecimal"") AS ""multiply"", ef_divide(""b"".""TestDecimal"", ""b0"".""TestDecimal"") AS ""divide"", ef_negate(""b"".""TestDecimal"") AS ""negate""
FROM ""BuiltInDataTypes"" AS ""b""
CROSS JOIN ""BuiltInDataTypes"" AS ""b0""
ORDER BY ""b"".""Id"", ""b0"".""Id""");
} Also, now some tests in // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.EntityFrameworkCore.Query
{
public class NorthwindAggregateOperatorsQuerySqliteTest : NorthwindAggregateOperatorsQueryRelationalTestBase<NorthwindQuerySqliteFixture<NoopModelCustomizer>>
{
public NorthwindAggregateOperatorsQuerySqliteTest(NorthwindQuerySqliteFixture<NoopModelCustomizer> fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
ClearLog();
//Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}
public override Task Sum_with_division_on_decimal(bool async)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Sum_with_division_on_decimal(async));
public override Task Sum_with_division_on_decimal_no_significant_digits(bool async)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Sum_with_division_on_decimal_no_significant_digits(async));
public override Task Average_with_division_on_decimal(bool async)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Average_with_division_on_decimal(async));
public override Task Average_with_division_on_decimal_no_significant_digits(bool async)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Average_with_division_on_decimal_no_significant_digits(async));
}
} and I will merge the PR |
Investigate NSE please |
@maumar thanks for the code snippets, @smitpatel will do so! |
in |
Test are now passing. No exceptions. |
@lokalmatador I have merged the PR. Thank you for the contribution! |
You're welcome! |
No description provided.